# -*- coding:utf-8 -*-
# @Time:2023/7/17 23:17
# @Author: cevent
# @name=fsToMp4

import requests
import os
import shutil

def merge_ts_files(url, num_of_ts_files):
    with open('output.mp4', 'ab') as f:
        for i in range(num_of_ts_files):
            ts_file_name = 'file_%03d.ts' % i
            r = requests.get(url + ts_file_name, stream=True)
            print('Merging ts file:', ts_file_name)
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
    print('Merging Completed')

    for i in range(num_of_ts_files):
        ts_file_name = 'file_%03d.ts' % i
        os.remove(ts_file_name)
    print("All ts files removed successfully")


if __name__=='main':

    merge_ts_files('https://dh5.cntv.myhwcdn.cn/asp/h5e/hls/2000/0303000a/3/default/fbad61a66e2445099d1c632bf3ebc596/',17)